using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;


namespace Elementum
{
    public static class ResourceManager
    {
        static ContentManager _content;
        static Dictionary<string, IDisposable> _resourceTable = new Dictionary<string, IDisposable>();
        static public RenderTarget2D _scene;

        static public ContentManager Content
        {
            get
            {
                if (_content == null)
                    throw new Exception("content not set!");
                return _content;
            }

            set
            {
                _content = new ContentManager(value.ServiceProvider, value.RootDirectory);
            }
        }

        static public void Dispose(){
            MusicManager.FreeAllMusic();
            _content.Unload();
            _resourceTable.Clear();
            _content = new ContentManager(_content.ServiceProvider, _content.RootDirectory);
        }

        static public Texture2D LoadTexture(string name)
        {
            if (_resourceTable.ContainsKey(name))
            {
                return (Texture2D)_resourceTable[name];
            }
            else
            {
                Texture2D t=null;
                try { 
                    t = Content.Load<Texture2D>(@name);
                }
                catch {
                    throw new Exception(name+" not found!");
                }
                
                 _resourceTable.Add(name, t);
                return t;
            }
        }

        static public SpriteFont LoadSpriteFont(string name)
        { 
            //if (_resourseTable.ContainsKey(name))
            //{
            //    return (SpriteFont)_resourseTable[name];
            //}
            //else
            //{
                SpriteFont t;
                t = Content.Load<SpriteFont>(@name);
                return t;
            //}
        }

        static public Song LoadSong(string name) {
            if (_resourceTable.ContainsKey(name))
            {
                return (Song)_resourceTable[name];
            }
            else
            {
                Song t;
                t = Content.Load<Song>(@name);
                _resourceTable.Add(name, t);
                return t;
            } 
        }

        static public SoundEffect LoadSoundEffect(string name)
        {

            if (_resourceTable.ContainsKey(name))
            {
                return (SoundEffect)_resourceTable[name];
            }
            else
            {
                SoundEffect t;
                t = Content.Load<SoundEffect>(@name);
                _resourceTable.Add(name, t);
                return t;
            }

        }
    }
}
